# coding:utf-8
from decimal import Decimal

from marshmallow import fields, validate
from marshmallow.validate import Range

from app.constant import Gender
from app.op.constant import UserLevel, DemandCate
from ..extensions import ma
from .utils import EmptyStrValidator


class PackSchema(ma.Schema):
    """
    created by chenhj at 2017/8/11
    """
    id = fields.Int()
    uid = fields.Int()
    bid_point = fields.Int()
    accum_bid_point = fields.Int()
    area_id = fields.List(fields.Int, attribute='attention_area_ids')
    created_at = fields.Int()
    updated_at = fields.Int()
    deleted_at = fields.Int()
    prov_list = fields.List(fields.Str, attribute='area_list')

pack_schema = PackSchema()


class DemandBidSchema(ma.Schema):
    """
    created by chenhj at 2017/8/14

    """
    id = fields.Int(required=True, error_messages={
        'required': '需求id不合法'
    })
    offer = fields.Int(required=True, error_messages={
        'required': '竞价点不合法'
    })

demand_bid_schema = DemandBidSchema()


class DemandIDInSchema(ma.Schema):
    """
    created by chenhj at 2017/8/14

    """
    demand_id = fields.Int(required=True, error_messages={
        'required': '需求id不合法'
    })

demand_id_in_schema = DemandIDInSchema()


class LevelProvSupplierInSchema(ma.Schema):
    """
    created by chenhj at 2017/8/11
    """
    prov = fields.Str(
        required=True,
        error_messages={
            'required': '省份不合法'
        },
        validate=EmptyStrValidator(error='省份不合法')
    )
    level = fields.Int(
        required=True,
        error_messages={
            'required': '工程商等级不合法'
        },
        validate=validate.OneOf(UserLevel.values(), error='工程商等级不合法')
    )

level_prov_sup_in_schema = LevelProvSupplierInSchema()


class UserDemandSchema(ma.Schema):
    """
    created by chenhj at 2017/8/14

    """
    uid = fields.Int(required=True, error_messages={
        'required': '用户ID不合法'
    })

user_demand_schema = UserDemandSchema()


class DemandInfoSchema(ma.Schema):
    id = fields.Int()
    id_ = fields.Int(attribute='id')
    uid = fields.Int()
    cate = fields.Int()
    malf = fields.Int()
    type = fields.Int()
    surr = fields.Int()
    size = fields.Float()
    color = fields.Int()
    span = fields.Str()
    img = fields.Str(attribute='img_url')
    prov = fields.Str()
    city = fields.Str()
    addr = fields.Str()
    audio = fields.Str(attribute='audio_url')
    audio_sensitive = fields.Bool()
    budget = fields.Float()
    source = fields.Str()
    bid_cnt = fields.Int()
    ip = fields.Str()
    sem = fields.Str()
    tag = fields.Int()
    status = fields.Int()
    remark = fields.Str()
    created_at = fields.Int()
    updated_at = fields.Int()
    published_at = fields.Int()
    scan_count = fields.Int()
    bid_start_point = fields.Int()
    published_count = fields.Int()
    for_gold = fields.Boolean(default=False)
    sem_keyword = fields.Str(dump_only=True)
    publish_remark = fields.Str(dump_only=True)
    # 爆灯汇币数(目前默认200)
    max_bid_point = fields.Constant(200, dump_only=True)
    # 首次竞价时长(目前默认300s)
    ongoing_bid_seconds = fields.Constant(300, dump_only=True)
    # 中标待联系时长(目前默认10*60, 600s)
    win_bid_contact_limit_seconds = fields.Constant(600, dump_only=True)

    # FIXME by chenhj at 2018/5/9 前端字段,怕去掉了前端报错,暂时留着
    product_type_name_display = fields.Constant('', dump_only=True)
    product_name_display = fields.Constant('', dump_only=True)
    deposit = fields.Constant(0, dump_only=True)
    product_amount = fields.Constant(0, dump_only=True)
    supply_date = fields.Constant('', dump_only=True)
    deadline = fields.Constant('', dump_only=True)

demand_info_schema = DemandInfoSchema()


class DemandCreateSchema(ma.Schema):
    """
    created by chenhj at 2017/8/15

    """
    mobile = fields.Str(required=True, error_messages={
        'required': '手机号不合法'
    }, validate=EmptyStrValidator(error='手机号不合法'))
    nickname = fields.Str()
    cate = fields.Int(required=True, error_messages={
        'required': '需求分类不合法'
    }, validate=validate.OneOf(DemandCate.values(), error='需求分类不合法'))
    prov = fields.Str(required=True, error_messages={
        'required': '省份不合法'
    }, validate=EmptyStrValidator(error='省份不合法'))
    city = fields.Str(required=True, error_messages={
        'required': '城市不合法'
    }, validate=EmptyStrValidator(error='城市不合法'))
    sem = fields.Str(default=None)
    malf = fields.Int()
    type = fields.Int()
    surr = fields.Int()
    color = fields.Int()
    span = fields.Str()
    # PC 代表pc站, MO代表mo站
    enterprise_source = fields.Str()

demand_create_schema = DemandCreateSchema()


class DemandEditSchema(ma.Schema):
    """
    created by chenhj at 2017/8/15

    """
    id = fields.Int(required=True, error_messages={
        'required': '项目id不合法'
    })
    malf = fields.Int(default=0)
    type = fields.Int(required=True, error_messages={
        'required': '分类不合法'
    }, validate=EmptyStrValidator(error='分类不合法'))
    surr = fields.Int(default=0)
    size = fields.Float(default=0)
    color = fields.Int(required=True, error_messages={
        'required': '屏幕颜色不合法'
    })
    span = fields.Str(required=True, error_messages={
        'required': '间距不合法'
    }, validate=EmptyStrValidator(error='间距不合法'))
    addr = fields.Str(required=True, error_messages={
        'required': '详细地址不合法'
    }, validate=EmptyStrValidator(error='详细地址不合法'))
    budget = fields.Float(default=0)
    remark = fields.Str(default='')

demand_edit_schema = DemandEditSchema()


class ActivateDemandSchema(ma.Schema):
    """
    created by chenhj at 2017/8/15
    激活需求
    """
    nickname = fields.Str(required=True, error_messages={
        'required': '昵称不合法'
    }, validate=EmptyStrValidator(error='昵称不合法'))
    prov = fields.Str(required=True, error_messages={
        'required': '省份不合法'
    }, validate=EmptyStrValidator(error='省份不合法'))
    city = fields.Str(required=True, error_messages={
        'required': '城市不合法'
    }, validate=EmptyStrValidator(error='城市不合法'))
    id = fields.Int(required=True, error_messages={
        'required': '项目id不合法'
    })
    malf = fields.Int(default=0)
    type = fields.Int(required=True, error_messages={
        'required': '分类不合法'
    })
    surr = fields.Int(default=0)
    size = fields.Float(default=0)
    color = fields.Int()
    span = fields.Str()

demand_activate_schema = ActivateDemandSchema()


class SupplierJoinSchema(ma.Schema):
    """
    created by chenhj at 2017/8/21

    """
    nickname = fields.Str(required=True, error_messages={
        'required': '昵称不合法'
    }, validate=EmptyStrValidator(error='昵称不合法'))
    mobile = fields.Str(required=True, error_messages={
        'required': '手机号不合法'
    }, validate=EmptyStrValidator(error='手机号不合法'))
    gender = fields.Int(required=True, error_messages={
        'required': '性别不合法'
    }, validate=validate.OneOf(Gender.values(), error='性别值不合法'))
    prov = fields.Str(required=True, error_messages={
        'required': '省份不合法'
    }, validate=EmptyStrValidator(error='省份不合法'))
    city = fields.Str(required=True, error_messages={
        'required': '城市不合法'
    }, validate=EmptyStrValidator(error='城市不合法'))
    addr = fields.Str()
    com = fields.Str()
    pos = fields.Str()

supplier_join_schema = SupplierJoinSchema()


class SupplierActivateSchema(ma.Schema):
    """
    created by chenhj at 2017/8/23

    """
    nickname = fields.Str(required=True, error_messages={
        'required': '昵称不合法'
    })

supplier_activate_schema = SupplierActivateSchema()


class UserTagSchema(ma.Schema):
    """
    create by ld_wu 2017-09-04
    工程商业务范围
    """
    install = fields.Boolean(default=False)
    repair = fields.Boolean(default=False)
    rent = fields.Boolean(default=False)
    purchase = fields.Boolean(default=False)

user_tag_schema = UserTagSchema()


class SupplierInviteUserSchema(ma.Schema):
    """
    created by chenhj at 2017/9/7

    """
    avatar = fields.Str(attribute='headimgurl')
    nickname = fields.Str()
    mobile = fields.Str()
    invite_time = fields.Int()
    invite_buy_pack = fields.Bool()
    pack_price = fields.Decimal(attribute='invite_pack_price', as_string=True)

supplier_invite_users_schema = SupplierInviteUserSchema(many=True)


class FeedbackSchema(ma.Schema):
    """
    create by ld_wu 2017-09-12
    信息反馈
    """
    demand_id = fields.Int(
        required=True,
        error_messages={
            'required': '缺少需求id'
        }
    )

    feedback = fields.Integer()
    # 反馈类型
    feedback_type = fields.Integer()
    feedback_content = fields.Str()

feedback_schema = FeedbackSchema()


class DemandOfferSchema(ma.Schema):
    """
    create by ld_wu 2017-09-26
    项目报价
    """
    id = fields.Int(
        required=True,
        error_messages={
            'required': '项目id为必填'
        },
        validate=EmptyStrValidator(error='项目id不合法')
    )
    offer = fields.Decimal(
        required=True,
        error_messages={
            'required': '项目报价为必填'
        },
        validate=Range(Decimal('0.01'))
    )

demand_offer_schema = DemandOfferSchema()


class VoucherSchema(ma.Schema):
    """
    created by chenhj at 2018/7/9

    """
    bid_point = fields.Int(dump_only=True)
    grant_date = fields.Date(dump_only=True)
    expired_date = fields.Date(dump_only=True)
    status = fields.Int(dump_only=True)

vouchers_schema = VoucherSchema(many=True)


class RedeemCodeSchema(ma.Schema):
    """
    created by chenhj at 2018/9/25

    """
    code = fields.Str(
        required=True,
        error_messages={
            'required': '兑换码必填'
        },
        validate=EmptyStrValidator(error='兑换码不合法')
    )

redeem_code_schema = RedeemCodeSchema()
